﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class 序列反射動態程式集_得到一個類的所有子類 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
         TestGetInterface.pp.ssss();
    }
}

#region 如何用反射获取继承自某类或实现某个接口的所有子类的集合




namespace TestGetInterface
{using System.Reflection;
    using System.Text;
    class pp
    {
        public static bool MyInterfaceFilter(Type typeObj, Object criteriaObj)
        {
            if (typeObj.ToString() == criteriaObj.ToString())
                return true;
            else
                return false;
        }
       public static void ssss()
        {
            Assembly assembly = Assembly.LoadFile(@"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll");//你的loadfile
            Type[] types = assembly.GetTypes();
            TypeFilter myFilter = new TypeFilter(MyInterfaceFilter);
            //String[] myInterfaceList = new String[2] 
            //          {"System.Collections.IEnumerable", 
            //          "System.Collections.ICollection"};
            String[] myInterfaceList = new String[1] 
                {
                "System.Collections.ICollection"};//支持ICollection
            foreach (Type type in types)
            {
                for (int index = 0; index < myInterfaceList.Length; index++  )
                {
                    Type[] myInterfaces = type.FindInterfaces(myFilter,
                            myInterfaceList[index]);
                    if (myInterfaces.Length > 0)
                    {
                        System.Web.HttpContext.Current.Response.Write(string.Format("\n{0} implements the interface {1}.",
                                type, myInterfaceList[index]));
                        for (int j = 0; j < myInterfaces.Length; j ++ )
                            System.Web.HttpContext.Current.Response.Write(string.Format("Interfaces supported: {0}.",
                                    myInterfaces[j].ToString()));
                    }
                    //else
                    //  Console.WriteLine(
                    //      "\n{0} does not implement the interface {1}.",
                    //      type, myInterfaceList[index]);
                }
                }
            } 
        }


    }
#endregion